package cn.edu.njtc.service.impl;

import cn.edu.njtc.common.ErrorMessage;
import cn.edu.njtc.entity.pojo.MedicalVisit;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.mapper.MedicalVisitMapper;
import cn.edu.njtc.service.MedicalVisitService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MedicalVisitServiceImpl implements MedicalVisitService {

    @Autowired
    private MedicalVisitMapper mapper;

    @Override
    public List<MedicalVisit> listAll() {
        return mapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<MedicalVisit> getById(Integer id) throws BizException {
        LambdaQueryWrapper<MedicalVisit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicalVisit::getUserId, id);
        List<MedicalVisit> medicalVisits = mapper.selectList(queryWrapper);
        if (medicalVisits.size() != 0 && medicalVisits != null) {
            return medicalVisits;
        }
        throw new BizException(ErrorMessage.VISIT_NOT_FOUND);
    }

    @Override
    public void add(MedicalVisit visit) throws BizException {
        int i = mapper.insert(visit);
        if (i == 0) {
            throw new BizException(ErrorMessage.VISIT_ADD_FAILED);
        }
    }

    @Override
    public void update(MedicalVisit visit) throws BizException {
        LambdaUpdateWrapper<MedicalVisit> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(MedicalVisit::getUserId, visit.getUserId())
                        .set(MedicalVisit::getDoctorId, visit.getDoctorId())
                                .set(MedicalVisit::getHospitalId, visit.getHospitalId())
                .eq(MedicalVisit::getVisitId, visit.getVisitId())
                .set(MedicalVisit::getVisitDate, visit.getVisitDate())
                .set(MedicalVisit::getDiagnosis, visit.getDiagnosis())
                .set(MedicalVisit::getPrescription, visit.getPrescription());
        int update = mapper.update(lambdaUpdateWrapper);
        if (update != 1) {
            throw new BizException(ErrorMessage.VISIT_UPDATE_FAILED);
        }
    }


    @Override
    public void delete(Integer id) {
        LambdaUpdateWrapper<MedicalVisit> lambdaUpdateWrapper = new UpdateWrapper<MedicalVisit>().lambda();
        lambdaUpdateWrapper.eq(MedicalVisit::getVisitId, id)
                .set(MedicalVisit::getDeleted, true);
        mapper.update(lambdaUpdateWrapper);// 逻辑删除
    }

    @Override
    public Page<MedicalVisit> pageList(int pageNum, int pageSize) {
        Page<MedicalVisit> page = new Page<>(pageNum, pageSize);
        mapper.selectPage(page, new QueryWrapper<>());
        System.out.println(page);
        return page;
    }
}
